package com.huika.renrench.control.my.activity;

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.Response.Listener;
import com.google.gson.reflect.TypeToken;
import com.hk.app.android.lib.http.params.FileTypeParam;
import com.huika.lib.bitmap.core.DisplayImageOptions;
import com.huika.lib.bitmap.core.ImageLoader;
import com.huika.lib.ui.widget.dialog.MMAlert;
import com.huika.lib.ui.widget.dialog.MMAlert.DialogOnItemClickListener;
import com.huika.lib.ui.widget.dialog.MMAlertDialog;
import com.huika.lib.utils.FileUtils;
import com.huika.renrench.GlobalApp;
import com.huika.renrench.R;
import com.huika.renrench.config.UrlConstants;
import com.huika.renrench.control.common.base.BaseAct;
import com.huika.renrench.control.my.account.SetPayPwdActivity;
import com.huika.renrench.control.my.bank.activity.MyBankCardActivity;
import com.huika.renrench.http.FormResultRequest;
import com.huika.renrench.http.JSONWrapAjaxParams;
import com.huika.renrench.http.RequestResult;
import com.huika.renrench.utils.FileTool;
import com.huika.renrench.utils.ImageTools;
import com.huika.renrench.view.RoundImageView;
import com.huika.renrench.view.TitleBarHelper;

public class AccountSettingActivity extends BaseAct{

	private RoundImageView img_user_pic;
	private LinearLayout set_img_user_pic;
	
	private LinearLayout lin_direction_register;//实名认证
	private TextView text_direction_register;
	private LinearLayout lin_modify_the_phone;//修改手机号
	private TextView text_modify_the_phone;
	private LinearLayout lin_my_bank_card;//我的银行卡
	private LinearLayout lin_change_password;//修改登录密码
	private LinearLayout lin_payment_code;//设置支付密码
	private TextView lin_user_exit;//账号退出
	
	private TextView payment_code_tv;
	
	public DisplayImageOptions options;
	
	@Override
	public void setRootView() {
		setContentView(R.layout.account_setting_activity);
	}
	
	@Override
	protected void initWidget() {
		super.initWidget();
		new TitleBarHelper(this, R.string.left_back, -1, R.string.account_setting);
		img_user_pic = (RoundImageView) findViewById(R.id.img_user_pic);
		set_img_user_pic = (LinearLayout) findViewById(R.id.set_img_user_pic);
		lin_direction_register = (LinearLayout) findViewById(R.id.lin_direction_register);
		text_direction_register = (TextView) findViewById(R.id.text_direction_register);
		lin_modify_the_phone = (LinearLayout) findViewById(R.id.lin_modify_the_phone);
		text_modify_the_phone = (TextView) findViewById(R.id.text_modify_the_phone);
		lin_my_bank_card = (LinearLayout) findViewById(R.id.lin_my_bank_card);
		lin_change_password = (LinearLayout) findViewById(R.id.lin_change_password);
		lin_payment_code = (LinearLayout) findViewById(R.id.lin_payment_code);
		lin_user_exit = (TextView) findViewById(R.id.lin_user_exit);
		payment_code_tv = (TextView) findViewById(R.id.set_code_tv);
		if (GlobalApp.userModel.getIsSetPayPwd()==1) {
			payment_code_tv.setText("修改交易密码fdsfds");
		}else{
			payment_code_tv.setText("设置交易密码fdsfa");
		}
		
		
		//创建头像下载
		options = new DisplayImageOptions.Builder()
		.showImageOnLoading(R.drawable.img_user_pic) 
		.showImageForEmptyUri(R.drawable.img_user_pic)  
		.showImageOnFail(R.drawable.img_user_pic) 
		.cacheInMemory(true)// 开启内存缓存
		.cacheOnDisk(true) // 开启硬盘缓存
		.resetViewBeforeLoading(false).build();
		
		setOnclickListener();
	}
	
	/** 控件添加监听器 */
	private void setOnclickListener() {
		set_img_user_pic.setOnClickListener(this);
		lin_direction_register.setOnClickListener(this);
		lin_modify_the_phone.setOnClickListener(this);
		lin_my_bank_card.setOnClickListener(this);
		lin_change_password.setOnClickListener(this);
		lin_payment_code.setOnClickListener(this);
		lin_user_exit.setOnClickListener(this);
	}
	
	@Override
	public void onResume() {//持续更新ui
		super.onResume();
	}
	
	@Override
	public void widgetClick(View v) {
		super.widgetClick(v);
		switch (v.getId()) {//修改头像
		case R.id.set_img_user_pic:
			selectUserpic();
			break;
		case R.id.lin_direction_register:
			showToastMsg("实名认证");
			break;
		case R.id.lin_modify_the_phone:
			showToastMsg("修改手机号");
			break;
		case R.id.lin_my_bank_card:
//			showToastMsg("我的银行卡");//我的银行卡入口
			showActivity(this, MyBankCardActivity.class);
			break; 
		case R.id.lin_change_password:
			showToastMsg("修改登录密码");
			break;
		case R.id.lin_payment_code:
			showActivity(this, SetPayPwdActivity.class);
			break;
		case R.id.lin_user_exit:
			getddd();
			break;
		}
	}
	
	private void getddd(){
		// 退出
		Dialog tipDialog = MMAlertDialog.ClearCacheDialog(this, getString(R.string.more_clear_cache_exit), null,
				new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						showToastMsg("退出成功");
					}
				});
		tipDialog.show();
	}

	private Dialog coverDialog;// 选择对话框
	protected File picFile;// 相片文件
	/** 从 拍照中选择 */
	public static final int ACTIVITY_RESULT_CROPCAMARA_WITH_DATA = 1;
	/** 从 相册中选择 */
	public static final int ACTIVITY_RESULT_CROPIMAGE_WITH_DATA = 2;
	// -------------------选择主页头像背景start---------------------------------
	private void selectUserpic() {
		coverDialog = MMAlert.createTwoChoicAlertNoTitle(this, R.string.common_camera, R.string.common_gallery, new DialogOnItemClickListener() {
			@Override
			public void onItemClickListener(View v, int position) {
				switch (position) {
				case 0:// 拍照
					openCamera();
					break;
				case 1:// 从相册选择
					openPhotoAlbum();
					break;
				default:
					break;
				}
			}
		});
		coverDialog.show();
	}
	
	private void openCamera() {
		Intent cameraIntent = null;
		picFile = ImageTools.initTempFile();
		cameraIntent = ImageTools.getTakeCameraIntent(Uri.fromFile(picFile));
		startActivityForResult(cameraIntent, ACTIVITY_RESULT_CROPCAMARA_WITH_DATA);
	}

	private void openPhotoAlbum() {
		picFile = ImageTools.initTempFile();
		Intent photoIntent = ImageTools.cropPhotoOfCompressFromGalleryIntent1(Uri.fromFile(picFile));
		startActivityForResult(photoIntent, ACTIVITY_RESULT_CROPIMAGE_WITH_DATA);
	}
	
	// -------------------选择头像背景end---------------------------------

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode != Activity.RESULT_OK) {
			return;
		}

		switch (requestCode) {
		case ACTIVITY_RESULT_CROPCAMARA_WITH_DATA:// 拍照
			if (picFile==null) {
				showToastMsg("拍照选择失败");
				return;
			}
			Intent intent = ImageTools.cropPhotoOfCompressIntent(Uri.fromFile(picFile));
			startActivityForResult(intent, ACTIVITY_RESULT_CROPIMAGE_WITH_DATA);
			break;
		case ACTIVITY_RESULT_CROPIMAGE_WITH_DATA:// 相册选择
			if (null == data) {
				return;
			}
			if (picFile==null) {
				showToastMsg("照片选择失败");
				return;
			}
			if (TextUtils.isEmpty(picFile.toString()) || !picFile.exists()) {
				showToastMsg("没有选择到图片");
				return;
			}
			setBackg();
		}
	}
	
	private void setBackg() {
		showLoadingDialog("");
		executeUpdateBackgInfoList();
	}
	
	/** 获取网络数据,刷新用户信息*/
	private void executeUpdateBackgInfoList(){ 
		try {
		FormResultRequest<String> request = new FormResultRequest<String>(UrlConstants.UPLOADIMG, getUpdateUserpicResp, this, new TypeToken<RequestResult<String>>() {}.getType());
		JSONWrapAjaxParams ajaxParams = new JSONWrapAjaxParams();//
		ajaxParams.putCommonTypeParam("userId", 100714);//userId
		if(picFile != null) {
		ajaxParams.putStringTypeParam("validateKey", FileTool.getMD5FromFile(picFile));
		ajaxParams.put(new FileTypeParam("fileName", picFile.getPath(), FileUtils.getMimeType(Uri.fromFile(new File(picFile.getPath())).toString())));
		}
		request.setRequestParams(ajaxParams);
		executeRequest(request);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private Listener<RequestResult<String>> getUpdateUserpicResp=new Listener<RequestResult<String>>() {
		
		@Override
		public void onResponse(RequestResult<String> result) {
			
			if (result.flag == 1) {
				if (result != null && result.getRs() != null) {
					String bean = result.getRs();
					ImageLoader.getInstance().displayImage(bean,img_user_pic, options);
				}
			} else {
				showToastMsg(result.msg);
			}
			dismissLoadingDialog();
			if (mOverlayLayout != null)
				hideOverLay();
		}
	};

}
